home *** CD-ROM | disk | FTP | other *** search
- /* Strip all comments and condense multiple white space charcters */
- /* from an ASM source */
-
- /* This program is designed to reduce the number of characters */
- /* which MASM loads when it INCLUDES a tested file. It tries to */
- /* retain the line numbering of the original file. It anticipates */
- /* that you will save a commented version and a stripped version. */
- /* If you discover errors, change the commented version and run this */
- /* program on it again. */
-
- /* Lew Paper */
- /* 3/1/87 */
-
- #include <stdio.h>
- #include <dos.h>
- #include <string.h>
-
- FILE *istream, *ostream;
-
- #define TAB 0x09
- #define RETURN 0x0d
- #define NEWLINE 0x0a
- #define SPACE 0x20
- #define FALSE 0
- #define TRUE 1
-
- #define MAXLINE 512
-
- #define ISBLANK(PTR) (*(PTR) == SPACE || *(PTR) == TAB)
- char close_error[] = "Error in closing file %s\n";
-
- extern int yesorno(char *);
-
- main (argc, argv)
- int argc;
- char *argv[];
- {
- int maxchar;
- static int terminator = 0;
- char *p1, *p2;
- static char line_in[MAXLINE] = {'A'};
- /* the input buffer */
- char line_out[MAXLINE]; /* the output buffer */
- int bypass_strip;
-
- if (argc != 3) {
- printf ("\nUsage: %s <input_file> <output_file>\n", argv[0]);
- exit(1);
- }
- /* try to open input file */
-
- if ((istream = freopen (argv[1], "rb", stdin)) == NULL) {
- printf ("\nCannot open input file %s", argv[1]);
- exit(2);
- }
- /* check whether to overwrite old output file */
-
- if (ostream = fopen (argv[2], "rb")) {
- fclose (ostream);
- printf ("\n%s already exists.\n", argv[2]);
- if (!yesorno("Do you want to overwrite it (Y or N)?")) {
- close_in (argv[1]);
- exit(3);
- }
- }
-
- /* try to open output file */
- if ((ostream = fopen (argv[2], "wb")) == NULL) {
- printf ("Cannot open output file %s", argv[2]);
- close_in (argv[1]);
- exit (4);
- }
-
- while ((fgets (line_in, MAXLINE, istream)) != (char *)NULL) {
-
- maxchar = strlen(line_in) - 1;
-
- /* Comment processing */
-
- if (terminator) {
- if (strchr(line_in, (char) terminator))
- /* End comment mode the */
- /* after terminator appears */
- terminator = 0;
- bypass_strip = 1; /* Only output cr, lf */
- line_out[0] = RETURN;
- line_out[1] = NEWLINE;
- line_out[2] = 0;
- }
- else { /* Check for "comment" */
- for (p1 = &line_in[0]; *p1 && ISBLANK(p1); ++p1);
- /* Bypass spaces */
- if (!strnicmp(p1, "comment", 7) && ISBLANK((p1 + 7))) {
- /* Check for "comment " */
- for (p1 += 8; *p1 && ISBLANK(p1); ++p1);
- /* Bypass spaces */
- terminator = *p1; /* Terminator is case */
- /* insensitive */
- if (terminator) /* terminator exists */
- if (strchr(++p1, (char) terminator))
- /* is it on the same line? */
- terminator = 0; /* Yes */
- bypass_strip = 1; /* Only output cr, lf */
- line_out[0] = RETURN;
- line_out[1] = NEWLINE;
- line_out[2] = 0;
- }
- else
- bypass_strip = 0; /* Strip the line */
- }
- /* Stripping */
- if (!bypass_strip) {
- p1 = &line_in[0]; /* p1 points at the next */
- /* character to read */
- p2 = &line_out[0]; /* p2 points at the next */
- /* character to write */
- while (*p1) {
- if (*p1 == ';') { /* Strip rest of line for comment */
- *p1 = 0; /* Terminate line_in here */
- if (p1 > &line_in[0] && ISBLANK((p1-1)))
- /* Can only be one space before */
- /* here, because the program */
- /* strips repeating spaces and */
- /* tabs */
- p2--;
- *p2++ = '\r';
- *p2++ = '\n';
- }
- else if (ISBLANK(p1)) { /* Strip repeating spaces and tabs */
- *p2 = *p1;
- for (++p1, ++p2; *p1 && ISBLANK(p1); ++p1);
- }
- else /* Copy character */
- *p2++ = *p1++;
- }
- *p2 = 0; /* End output line */
-
- }
-
- /* Output line */
- if (fputs (line_out, ostream)) {
- printf ("Error writing file %s\n", argv[2]);
- printf ("This is usually a full output disk\n");
- (void) close_both (argv[1], argv[2]);
- exit (5);
- }
- }
-
- if (close_both (argv[1], argv[2]))
- exit(6);
- printf ("Stripped file %s to file %s\n", argv[1], argv[2]);
- exit (0);
- }
-
- output_error (in_name, out_name)
- char *in_name, *out_name;
-
- {
- printf ("Error writing file %s\n", out_name);
- printf ("This is usually a full output disk\n");
- (void) close_both (in_name, out_name);
- exit (5);
- }
-
- close_in (in_name)
- char *in_name;
-
- {
- if (fclose (istream))
- printf (close_error, in_name);
- }
-
- int close_both (in_name, out_name)
- char *in_name, *out_name;
-
- {
- int ret_i, ret_o;
-
- if (ret_i = fclose (istream))
- printf (close_error, in_name);
- if (ret_o = fclose (ostream))
- printf (close_error, out_name);
- return (ret_i || ret_o);
- }